package com.lianxueda;

import java.util.Scanner;

/*
吃货联盟
* */
public class foodieDemo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String[] names = new String[4];//保存送餐人姓名
        String[] dishMegs = new String[4];//保存所选信息，包括菜品名及份数
        int[] times = new int[4];//保存送餐时间
        String[] addresses = new String[4];//保存送餐地址
        int[] states = new int[4];//保存订单状态：0表示已预定，1表示已完成
        double[] sumPrices = new double[4];//保存订单的总金额

        String[] dishNames = {"红烧带鱼", "鱼香肉丝", "时令鲜蔬"};//菜品名称
        double[] prices = new double[]{38.0, 20.0, 10.0};//菜品单价
        int[] praiseNums = new int[3];//点赞数

        //初始化第一条订单信息
        names[0] = "张晴";
        dishMegs[0] = "红烧带鱼2份";
        times[0] = 12;
        addresses[0] = "天成路207号";
        sumPrices[0] = 76.0;
        states[0] = 1;

        //初始化第二条订单信息
        names[1] = "张晴";
        dishMegs[1] = "鱼香肉丝2份";
        times[1] = 18;
        addresses[1] = "天成路207号";
        sumPrices[1] = 45.0;
        states[1] = 0;

        System.out.println("----------欢迎使用吃货联盟订餐系统----------");
        System.out.println("********************************************");
        int num = -1;
        boolean flag = false;
        int choose = 0;
        do {
            System.out.println("1、我要订餐");
            System.out.println("2、查看餐袋");
            System.out.println("3、签收订单");
            System.out.println("4、删除订单");
            System.out.println("5、我要点赞");
            System.out.println("6、退出系统");
            System.out.println("********************************************");
            System.out.print("请选择您的需求:");
            if (input.hasNextInt()) {
                choose = input.nextInt();//记录用户选择的功能编号
            } else {
                System.out.println("你输入的菜单无效!请重新输入:");
                input = new Scanner(System.in);//防止input扫描器对象失效，导致无限循环
                choose = input.nextInt();
            }

            switch (choose) {
                case 1:
                    for (int i = 0; i < names.length; i++) {
                        System.out.println("***我要订餐***");
                        System.out.print("请输入订餐人姓名:");
                        String name = input.next();
                        for (int j = 0; j < dishNames.length; j++) {
                            System.out.println("序号\t\t\t菜名\t\t\t\t单价\t\t\t\t点赞数");
                            System.out.println((j + 1) + "\t\t\t" + dishNames[j] + "\t\t\t" + prices[j] + "\t\t\t" + praiseNums[j]);
                        }
                        System.out.println("请选择您要点的菜品编号:");
                        int number = input.nextInt();
                        System.out.println("请选择您需要的份数:");
                        int copies = input.nextInt();
                        System.out.println("请输入送餐时间(送餐时间是10点至20点间整点送餐):");
                        int time = input.nextInt();
                        if (time < 10 || time > 20) {
                            System.out.println("您的订餐时间已超出范围,请重新输入:");
                            int time1 = input.nextInt();
                        }
                        System.out.println("请输入您的订餐地址:");
                        String address = input.next();
                        System.out.println("订餐成功!");
                        String disMeg = dishNames[number - 1];
                        System.out.println("您订的是:" + disMeg + " " + copies + "份");
                        System.out.println("送餐时间是:" + time + "点");
                        int scf = 5;
                        double money = prices[number - 1] * copies;
                        double sum = money + scf;
                        System.out.println("餐费:" + money + ",送餐费:" + scf + "元" + ",总计:" + sum + "元");
                        names[i] = name;

                        dishMegs[i] = disMeg;
                        times[i] = time;
                        addresses[i] = address;
                        sumPrices[i] = sum;
                        break;
                    }
                    break;

                case 2:
                    System.out.println("***查看餐袋***");
                    //显示当前位置(面包屑导航)
                    System.out.println("当前位置:吃货联盟订餐系统 >> 查看餐袋");
                    //表头
                    System.out.println("序号\t\t\t订餐人\t\t\t餐品信息\t\t\t送餐日期\t\t\t送餐地址\t\t\t\t总金额\t\t\t订单状态");
                    //显示订单数据
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null) {
                            String state = (states[i] == 0) ? "已预定" : "已完成";
                            String date = times[i] + "点";
                            String sumPrice = sumPrices[i] + "元";
                            System.out.println((i + 1) + "\t\t\t" + names[i] + "\t\t\t" + dishMegs[i] +
                                    "\t\t\t" + date + "\t\t\t" + addresses[i] + "\t\t\t" + sumPrice + "\t\t\t" + state);
                        }
                    }
                    break;
                case 3:
                    System.out.println("***签收订单***");
                    boolean isSignFind = false;//找到要签收的订单
                    System.out.println("请选择您要签收的订单序号:");
                    int signOrderId = input.nextInt();
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null && states[i] == 0 && signOrderId == i + 1) {
                            states[i] = 1;//将状态值置为已完成
                            System.out.println("订单签收成功!");
                            isSignFind = true;//标签已找到订单

                        } else if (names[i] != null && states[i] == 1 && signOrderId == i + 1) {
                            System.out.println("您选择的订单已完成签收,不能再次签收");
                            isSignFind = true;//标签已找到订单
                        }
                    }
                    if (!isSignFind) {
                        System.out.println("您选择的订单不存在");
                    }
                    break;
                case 4:
                    System.out.println("***删除订单***");
                    boolean isDeiFind = false;//标记是否找到要删除的订单
                    System.out.println("请输入要删除的订单序号:");
                    int delId = input.nextInt();
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null && states[i] == 1 && delId == i + 1) {
                            isDeiFind = true;//标记已找到订单
                            //执行删除操作:删除位置后的元素依次前移
                            for (int j = 0; j < names.length; j++) {
                                names[j] = names[j + 1];
                                dishMegs[j] = dishMegs[j + 1];
                                times[j] = times[j + 1];
                                addresses[j] = addresses[j + 1];
                                states[j] = states[j + 1];
                                sumPrices[j] = sumPrices[j + 1];
                            }
                            //最后一位清空
                            int endIndex = names.length - 1;
                            names[endIndex] = null;
                            dishMegs[endIndex] = null;
                            times[endIndex] = 0;
                            addresses[endIndex] = null;
                            states[endIndex] = 0;
                            sumPrices[endIndex] = 0;
                            System.out.println("删除订单成功!");
                            break;
                        } else if (names[i] != null && states[i] == 0 && delId == i + 1) {
                            System.out.println("您选择的订单未签收,不能删除!");
                            isDeiFind = true;
                            break;
                        }
                    }
                    if (!isDeiFind) {
                        System.out.println("您要删除的订单不存在!");
                    }
                    break;
                case 5:
                    System.out.println("***我要点赞***");
                    System.out.println("序号\t\t\t菜名\t\t\t\t单价");
                    for (int i = 0; i < dishNames.length; i++) {
                        String price = prices[i] + "元";
                        String priaiseNum = (praiseNums[i]) > 0 ? praiseNums[i] + "赞" : "";
                        System.out.println((i + 1) + "\t\t\t" + dishNames[i] + "\t\t\t" + price + "\t\t\t" + priaiseNum);
                    }
                    System.out.println("请选择您要点赞的菜品序号:");
                    int priaiseNum = input.nextInt();
                    praiseNums[priaiseNum - 1]++;//点赞数加1
                    System.out.println("点赞成功!");
                    break;
                case 6:
                    System.out.println("***退出系统***");
                    return;
            }

            if (!flag) {
                System.out.println("输入0返回:");
                num = input.nextInt();
            } else {
                break;
            }
        } while (num == 0);
    }
}
